package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Wares;
import com.lin.pojo.WaresOrder;
import com.lin.services.ReduceReportService;
import com.lin.services.WaresService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ClassName ReduceReportController
 * Package com.lin.controller
 *
 * @author hellcat
 * Date 2020/3/18
 * Description:进货报表管理
 * 🐾
 */
@Controller
@RequestMapping("reduceReport")
public class ReduceReportController {
    @Autowired
    private ReduceReportService reduceReportService;
    @Autowired
    private WaresService waresService;

    @MyLog("查看退货报表")
    @RequiresPermissions("report:reduceReport:list")
    @RequestMapping(value = "/toReduceReport", method = RequestMethod.GET)
    public String toReduceReport(HttpServletRequest request) {
        request.setAttribute("ddId", "ReduceReport");
        return "reduceReportMain";
    }

    @RequiresPermissions("report:reduceReport:list")
    @RequestMapping(value = "/toReduceReportDetails", method = RequestMethod.POST)
    public String toReduceReportDetails(HttpServletRequest request) {
        request.setAttribute("ddId", "ReduceReport");
        String id = request.getParameter("waresId");
        String date = request.getParameter("date");
        request.setAttribute("searchDate", date);
        Wares wares = waresService.getCategoryById(id);
        WaresOrder order = reduceReportService.getWaresById(id, date);
        request.setAttribute("wares", wares);
        request.setAttribute("order", order);
        return "reduceReportDetails";

    }

    @ResponseBody
    @RequiresPermissions("report:reduceReport:list")
    @RequestMapping(value = "/getReduceReportDetails", method = RequestMethod.POST)
    public Map<String, Object> getReduceReportDetails(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchDate = request.getParameter("searchDate");
        String searchWaresId = request.getParameter("searchWaresId");
        List<WaresOrder> list = reduceReportService.getReduceReportDetails(searchDate, searchWaresId);
        return PageUnit.Page(list, page, limit);
    }

    @ResponseBody
    @RequiresPermissions("report:reduceReport:list")
    @RequestMapping(value = "/getReduceReport", method = RequestMethod.POST)
    public Map<String, Object> getReduceReport(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String searchDate = request.getParameter("searchDate");
        String searchWaresId = request.getParameter("searchWaresId");
        List<WaresOrder> list = reduceReportService.getReduceReport(searchDate, searchWaresId);
        return PageUnit.Page(list, page, limit);
    }

    @ResponseBody
    @RequiresPermissions("report:reduceReport:list")
    @RequestMapping(value = "/getChartsData", method = RequestMethod.POST)
    public Map<String, Object> getChartsData(HttpServletRequest request) {
        String date = request.getParameter("date");
        return reduceReportService.getChartsData(date);
    }
}
